import 'package:p_location/common/obj/dept_filter_option.dart';
import 'package:p_location/common/ui/base_filter_head_list_view.dart';
import 'package:p_location/home/constant/home_http_url.dart';
import 'package:p_location/home/obj/area_detail_info_item.dart';
import 'package:p_location/home/res/tab_single_area_info_res.dart';
import 'package:p_location/home/widget/cell/cell_area_car_info.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/utils/string_util.dart';

class TabHomeAreaCarInfo extends BaseFilterHeadListView {
  final bool isFromSearch;
  final bool isHiddenAppbar;
  final bool isHiddenBack;

  final int areaTreeId;

  @override
  BaseFilterHeadListViewState<BaseFilterHeadListView> getState() {
    return _FilterHeadListViewState();
  }

  const TabHomeAreaCarInfo(this.areaTreeId,
      {this.isFromSearch = false,
      this.isHiddenAppbar = true,
      this.isHiddenBack = true})
      : super(isHiddenAppbar: isHiddenAppbar, isHiddenBack: isHiddenBack);
}

class _FilterHeadListViewState
    extends BaseFilterHeadListViewState<TabHomeAreaCarInfo> {
  bool nextPageFlag = false;

  List<DeptFilterOption> typeOptions = [];

  List survivedList = [];

  @override
  void initState() {
    super.initState();
  }

  @override
  initDropMenus() {
    initDropMenuView(0, [DeptFilterOption(deptCode: "全部", deptName: "全部")]);
  }

  @override
  List<String> getTabTitles() {
    return ['车辆类型'];
  }

  @override
  String getTitle() {
    return '车辆报警';
  }

  @override
  List<String> getFilterKeys() {
    return ['carType'];
  }

  @override
  bool hasNextPage() {
    return true;
  }

  @override
  getActions() {
    return [];
  }

  @override
  void realRequest() async {
//    params["areaTreeId1"] = widget.areaTreeId;
    print("===========");
    print(params);
    print("===========");
    if (params != null &&
        StringUtil.isNotEmpty(params["carType"]) &&
        params["carType"] != "全部") {
      isLoading = false;
      survivedList = [];
      setState(() {
        lists.forEach((element) {
          if (element != null && element.type == params["carType"]) {
            survivedList.add(element);
          }
        });
      });
    } else {
      if (params != null && params["carType"] == "全部") {
        isLoading = false;
        survivedList = lists;
      } else {
        await DioHttpClient.requestPost(
            context,
            '${HomeHttpUrl.SINGLE_AREA_DETAIL_URL}?areaTreeId=${widget.areaTreeId}',
            params,
            this);
      }
    }
  }

  @override
  bool isInnerFilter() {
    return true;
  }

  @override
  int outerLength() {
    return survivedList?.length ?? 0;
  }

  @override
  List getList(response) {
    var resultList = [];
    if (response != null && response.data != null) {
      TabSingleAreaInfoRes listRes =
          TabSingleAreaInfoRes.fromJson(response.data);
      if (listRes != null && listRes.data != null) {
        AreaDetailInfoItem entire = listRes.data;
        if (entire.vehiclelist != null) {
          resultList.addAll(entire.vehiclelist);
        }
        typeOptions = [];
        typeOptions.add(DeptFilterOption(deptCode: "全部", deptName: "全部"));
        if(entire.vehiclelist != null){
          entire.vehiclelist.forEach((element) {
            if (StringUtil.isNotEmpty(element.type)) {
              DeptFilterOption find1 = typeOptions.firstWhere(
                      (e1) => e1.deptName == element.type,
                  orElse: () => null);
              if (find1 == null) {
                typeOptions.add(DeptFilterOption(
                    deptCode: element.type, deptName: element.type));
              }
            }

            initDropMenuView(0, typeOptions);
          });
        }

//        if (entire.carOwner != null) {
//          resultList.addAll(entire.carOwner);
//        }
//        if (entire.carVisit != null) {
//          resultList.addAll(entire.carVisit);
//        }
        survivedList = resultList;
        return resultList;
      }
    }
    survivedList = resultList;
    return resultList;
  }

  @override
  createListViewItem(index) {
    return CellAreaCarInfo(survivedList[index]);
  }
}
